package com.hybao.security;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hybao.CookieUtil;

public class AuthHandler {
	private static final String COOKIE_NAME_UID = "HYBAO_LOGIN_UID";
	private static final String COOKIE_NAME_NICK = "HYBAO_LOGIN_NICK";
	private static final String COOKIE_PATH = "/";
	
	public static LoginUser getUserFromCookie(HttpServletRequest request) {
		String uid = CookieUtil.getCookieValue(request, COOKIE_NAME_UID);
		if (uid == null){
			return null;
		}
		String nick = CookieUtil.getCookieValue(request, COOKIE_NAME_NICK);
		LoginUser user = new LoginUser();
		user.setUserDomainId(uid);
		user.setUserNick(nick);
		return user;
	}
	
	public static void setUserIntoCookie(HttpServletResponse response, String login_uid, String login_nick){
		CookieUtil.setCookie(response, COOKIE_NAME_UID, login_uid,COOKIE_PATH);
		CookieUtil.setCookie(response, COOKIE_NAME_NICK, login_nick,COOKIE_PATH);
	}
}
